package cn.ac.cintcm.prespring.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 操作以id:id1,id2,id3....格式存贮的数据文件
 * 其中id不能为-1
 * @author 何前锋 Franklin He(franklinheqf@gmail.com)
 *
 */
public class FileUtil {
    /**
     * 从指定的文件中读取行数据，形成以行为存贮单位的链表
     * @param fileName 文件名称
     * @return 存贮每一行的链表
     * @throws IOException
     */
    public static List<String> read(String fileName) throws IOException {
        return read(Config.DATA_DIR, fileName);
    }
    
    public static List<String> read(String directory, String fileName) throws IOException {
        List<String> stringList = new ArrayList<String>();
        File inputFile = new File(directory, fileName);
        BufferedReader reader = new BufferedReader(new FileReader(inputFile));
        String line = reader.readLine();
        while (line != null) {
            stringList.add(line);
            line = reader.readLine();
        }
        reader.close();
        return stringList;
    }   
    /**
     * 将形如id:id1,id2,id3....的数据文件读取成为id为key的，id1, id2,...为链表的存贮结构
     * @param fileName 存贮上述格式的文件名
     * @return id与链表对应的数据结构
     * @throws IOException
     */
    public static HashMap<Integer, List<Integer>> structureRead(String fileName)
                    throws IOException {
        List<String> stringList = read(fileName);
        HashMap<Integer, List<Integer>> idMap = null;
        if (stringList != null) {
            idMap = new HashMap<Integer, List<Integer>>();
            for (String s : stringList) {
                List<Integer> idList = new ArrayList<Integer>();
                int id = parseLine(s, idList);
                if (id != -1) {
                    idMap.put(id, idList);
                }
            }
        }
        return idMap;
    }

    
    /**
     * 将形如：id:id1,id2,id3....的数据装入链表
     * @param s id组成的字符串
     * @param list 冒号后的id字符串形成的链表,传入参数的返回
     * @return 冒号前的id，如果s为空，返回-1
     */
    public static int parseLine(String s, List<Integer> list) {
        int id = -1;
        if (s != null) {
            String[] ps = s.split(Config.FIELD_SPLITTER);
            if (ps.length == 2) {
                String[] elements = ps[1].split(Config.ITEM_SPLITTER);
                for (String es : elements) {
                    list.add(Integer.parseInt(es));
                }
                id = Integer.parseInt(ps[0]);
            }
        }
        return id;
    }
    /**
     * 将形如：id:id1,id2,id3....的数据装入链表
     * @param s id组成的字符串
     * @param list 冒号后的id字符串形成的链表,传入参数的返回
     * @return 冒号前的id，如果s为空，返回-1
     */
    public static String parseLineStr(String s, List<String> list) {
        String item = null;
        if (s != null) {
            String[] ps = s.split(Config.FIELD_SPLITTER);
            if (ps.length == 2) {
                String[] elements = ps[1].split(Config.ITEM_SPLITTER);
                for (String es : elements) {
                    list.add(es);
                }
                item = ps[0];
            }
        }
        return item;
    }

    public static void write(StringBuilder builder, String fileName) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
        writer.write(builder.toString());
        writer.close();
    }

}
